﻿using Mengsk.ProxyDelivery.Converters;
using Mengsk.ProxyDelivery.Domain;
using Mengsk.ProxyDelivery.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Mengsk.ProxyDelivery.App.UI.Goods
{
    /// <summary>
    /// GiftEditWindow.xaml 的交互逻辑
    /// </summary>
    public partial class GiftEditWindow : Window
    {
        private GiftService giftService = ServiceContainer.GetService<GiftService>();
        private string imagePath = null;

        public bool NeedRefresh { get; set; }

        public Gift Gift { get; set; }

        public GiftEditWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                if (this.Gift == null)
                {
                    this.Gift = new Gift();
                }
                this.tbName.Text = this.Gift.Name;
                this.tbPrice.Text = this.Gift.Price.ToString("F2");
                this.tbBuyInPrice.Text = this.Gift.BuyInPrice.ToString("F2");
                if (string.IsNullOrWhiteSpace(this.Gift.Image) == false)
                {
                    this.img.Source = (new WebUrlImageConverter()).Convert(this.Gift.Image, null, null, null) as ImageSource;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            string name = this.tbName.Text.Trim();
            string price = this.tbPrice.Text.Trim();
            string buyInPrice = this.tbBuyInPrice.Text.Trim();

            try
            {
                float fPrice = float.Parse(price);
                float fBuyInPrice = float.Parse(buyInPrice);

                if (string.IsNullOrWhiteSpace(name))
                {
                    MessageBox.Show("名称不能为空");
                    return;
                }

                if (fPrice < 0 || fBuyInPrice < 0)
                {
                    MessageBox.Show("价格不能小于0");
                    return;
                }
                this.Gift.Name = name;
                this.Gift.Price = fPrice;
                this.Gift.BuyInPrice = fBuyInPrice;
                this.Gift.Image = imagePath;
                if (this.Gift.Id > 0)
                {
                    this.giftService.Update(this.Gift);
                }
                else
                {
                    this.Gift.Id = this.giftService.Insert(this.Gift);
                }
                this.DialogResult = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnImage_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog ofd = new Microsoft.Win32.OpenFileDialog();
            bool? ret = ofd.ShowDialog();
            if (ret == null || ret.Value == false)
            {
                return;
            }

            this.img.Source = new BitmapImage(new Uri(ofd.FileName));
            this.imagePath = ofd.FileName;
        }
    }
}
